import ctypes
import serial
import time

def drive():
    # START SENDINPUT TYPE DECLARATIONS
    PUL = ctypes.POINTER(ctypes.c_ulong)
    class KeyBdInput(ctypes.Structure):
        _fields_ = [("wVk", ctypes.c_ushort),
                 ("wScan", ctypes.c_ushort),
                 ("dwFlags", ctypes.c_ulong),
                 ("time", ctypes.c_ulong),
                 ("dwExtraInfo", PUL)]

    class HardwareInput(ctypes.Structure):
        _fields_ = [("uMsg", ctypes.c_ulong),
                 ("wParamL", ctypes.c_short),
                 ("wParamH", ctypes.c_ushort)]

    class MouseInput(ctypes.Structure):
        _fields_ = [("dx", ctypes.c_long),
                 ("dy", ctypes.c_long),
                 ("mouseData", ctypes.c_ulong),
                 ("dwFlags", ctypes.c_ulong),
                 ("time",ctypes.c_ulong),
                 ("dwExtraInfo", PUL)]

    class Input_I(ctypes.Union):
        _fields_ = [("ki", KeyBdInput),
                  ("mi", MouseInput),
                  ("hi", HardwareInput)]

    class Input(ctypes.Structure):
        _fields_ = [("type", ctypes.c_ulong),
                 ("ii", Input_I)]

    class POINT(ctypes.Structure):
        _fields_ = [("x", ctypes.c_ulong),
                 ("y", ctypes.c_ulong)]
    # END SENDINPUT TYPE DECLARATIONS

    FInputs = Input * 2
    extra = ctypes.c_ulong(0)

    click = Input_I()
    click.mi = MouseInput(0, 0, 0, 2, 0, ctypes.pointer(extra))
    release = Input_I()
    release.mi = MouseInput(0, 0, 0, 4, 0, ctypes.pointer(extra))

    x = FInputs( (0, click), (0, release) )
    
    ##FInputs = Input
    ##extra = ctypes.c_ulong(0)

    ##click = Input_I()
    ##click.mi = MouseInput(0, 0, 0, 2, 0, pointer(extra))
    ##release = Input_I()
    ##release.mi = MouseInput(0, 0, 0, 4, 0, pointer(extra))

    ##x = FInputs((0, click))
    ##y = FInputs((0, release))
    ###ctypes.windll.user32.SendInput(2, pointer(x), sizeof(x[0]))

    line = serial.Serial(3, 19200)
    raw = ""
    data = ["","","",""]
    buttonstat = ["",""]
    cursornow = POINT()
    lasttime = 0

    while True:
        raw = line.readline()
        data = raw[:-2].split(",")
        if len(data) > 4:
            raw = line.readline()
            data = raw[:-2].split(",")

        if int(data[0]) > 180:
            ctypes.windll.user32.GetCursorPos(ctypes.byref(cursornow))
            ctypes.windll.user32.SetCursorPos(cursornow.x + 10, cursornow.y)

        if int(data[0]) < 80:
            ctypes.windll.user32.GetCursorPos(ctypes.byref(cursornow))
            ctypes.windll.user32.SetCursorPos(cursornow.x - 10, cursornow.y)

        if int(data[1]) > 180:
            ctypes.windll.user32.GetCursorPos(ctypes.byref(cursornow))
            ctypes.windll.user32.SetCursorPos(cursornow.x, cursornow.y - 10)

        if int(data[1]) < 80:
            ctypes.windll.user32.GetCursorPos(ctypes.byref(cursornow))
            ctypes.windll.user32.SetCursorPos(cursornow.x, cursornow.y + 10)

        if data[2] == "1" and (time.clock() - lasttime >= 0.5) : #and data[2] != buttonstat[0]:
            buttonstat[0] = "1"
            ctypes.windll.user32.SendInput(2, ctypes.pointer(x), ctypes.sizeof(x[0]))
            lasttime = time.clock()

        #if data[2] == "0" and data[2] != buttonstat[0]:
        #    buttonstat[0] = "0"
        #    ctypes.windll.user32.SendInput(1, pointer(y), sizeof(y))
